package com.iplant.basic.serviceImpl.dao.fmc;

import com.iplant.basic.service.po.fmc.FMCResource;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class FMCResourceDAO extends BaseDAO {

	private static FMCResourceDAO Instance = null;
	private static Logger logger = LoggerFactory.getLogger(FMCResourceDAO.class);

	private FMCResourceDAO() {
		super();

		setTableName("fmc_resource");
	}

	public static FMCResourceDAO getInstance() {
		if (Instance == null)
			Instance = new FMCResourceDAO();
		return Instance;
	}

	public void FMC_UpdateResource(BMSEmployee wLoginUser, FMCResource wResource, OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);

		try {
			if (wResource == null || wResource.StationID <= 0 || wResource.ResourceID <= 0 || wResource.TypeID <= 0
					|| wResource.WorkTypeID <= 0) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}
			FMCResource wResourceO = this.FMC_CheckResource(wLoginUser, wResource, wErrorCode);
			if (wResourceO.ID > 0) {
				wErrorCode.set(ExceptionTypes.Duplication.getValue());
			}
			if (wErrorCode.Result != 0)
				return;

			Map<String, Object> wParams = new HashMap<String, Object>();

			wParams.put("WorkTypeID", wResource.WorkTypeID);
			wParams.put("StationID", wResource.StationID);

			wParams.put("ProductID", wResource.ProductID);

			wParams.put("TypeID", wResource.TypeID);
			wParams.put("ResourceID", wResource.ResourceID);

			wParams.put("Description", wResource.Description);

			wParams.put("EditorID", wLoginUser.getID());
			wParams.put("EditTime", Calendar.getInstance());
			wParams.put("Active", wResource.Active);

			if (wResource.ID > 0) {
				wParams.put("ID", wResource.ID);
				super.Update(super.getTableName("fmc_resource"), "ID", wParams);
			} else {
				wParams.put("CreatorID", wLoginUser.getID());
				wParams.put("CreateTime", Calendar.getInstance());
				wResource.ID = super.Insert(super.getTableName("fmc_resource"), wParams);
			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("FMC_UpdateResource", ex);
		}
	}

	public void FMC_ActiveResource(BMSEmployee wLoginUser, FMCResource wResource, int wActive,
			OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);
		// 判断客户信息是否存在(中国：统一社会信用代码，国外:提醒是否有重复）

		try {
			if (wResource == null || wResource.ID <= 0) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}

			Map<String, Object> wParams = new HashMap<String, Object>();

			wParams.put("ID", wResource.ID);
			wParams.put("EditorID", wLoginUser.getID());
			wParams.put("EditTime", Calendar.getInstance());
			wParams.put("Active", wActive);
			super.Update(super.getTableName("fmc_resource"), "ID", wParams);

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());

			logger.error("FMC_ActiveResource", ex);
		}
	}

	public List<FMCResource> FMC_QueryResourceList(BMSEmployee wLoginUser, String wResourceCode, String wResourceName,
			List<Integer> wStationID, int wWorkType, int wTypeID, int wProductID, int wResourceID, int wActive,
			Pagination wPagination, OutResult<Integer> wErrorCode) {
		return this.FMC_QueryResourceList(wLoginUser, -1, wResourceCode, wResourceName, wStationID, wWorkType, wTypeID,
				wProductID, wResourceID, wActive, wPagination, wErrorCode);
	}

	private List<FMCResource> FMC_QueryResourceList(BMSEmployee wLoginUser, int wID, String wResourceCode,
			String wResourceName, List<Integer> wStationID, int wWorkTypeID, int wTypeID, int wProductID,
			int wResourceID, int wActive, Pagination wPagination, OutResult<Integer> wErrorCode) {
		List<FMCResource> wFMCResourceList = new ArrayList<FMCResource>();
		wErrorCode.set(0);

		try {

			if (wStationID == null)
				wStationID = new ArrayList<Integer>();
			wStationID.removeIf(p -> p <= 0);
			// Step02:查询
			Map<String, Object> wParams = new HashMap<String, Object>();
			String wSQLText = "";
			wSQLText = StringUtils.Format("Select t.*,t1.Name as WorkTypeName,t1.Code as WorkTypeCode,"
					+ " t2.Name as StationName,t2.Code as StationCode,"
					+ " t3.Name as CreatorName ,t4.Name as EditorName ,t7.Name as ProductName,"
					+ " t7.Code as ProductNo,t5.Name as EmployeeName,t5.LoginID as EmployeeCode,"
					+ " t6.Name as TeamName,t6.Code as TeamCode  from {0}.fmc_resource t  "
					+ " left join {0}.bms_worktype t1 on t.WorkTypeID=t1.ID "
					+ " left join {0}.fmc_station t2 on t.StationID=t2.ID "
					+ " left join {0}.mbs_user t3 on t.CreatorID=t3.ID "
					+ " left join {0}.mbs_user t4 on t.EditorID=t4.ID "
					+ " left join {0}.mbs_user t5 on t.ResourceID=t5.ID and t.TypeID=1 and t5.Active=1 "
					+ " left join {0}.bms_team t6 on t.ResourceID=t6.ID and t.TypeID=2 and t6.Active=1  "
					+ " left join {0}.fpc_product t7 on t.ProductID=t7.ID "
					+ " where (:ID<=0 or  t.ID=:ID) AND (:WorkTypeID<=0 or  t.WorkTypeID=:WorkTypeID)"
					+ " AND (:TypeID<=0 or  t.TypeID=:TypeID)  AND (:ProductID<=0 or  t.ProductID=:ProductID) "
					+ " AND (:ResourceID<=0 or  t.ResourceID=:ResourceID) AND (:Active<0 or  t.Active=:Active)"
					+ " AND (:ResourceName = '''' or  t6.Name like :ResourceName or  t5.Name like :ResourceName) "
					+ " AND (:ResourceCode =''''  or  t6.Code=:ResourceCode  or t5.LoginID=:ResourceCode )"
					+ " AND (:StationID = '''' or  t.StationID IN ({1}) ) group by t.ID ", super.getDBName(),
					wStationID.size() > 0 ? StringUtils.Join(",", wStationID) : "0");

			wSQLText = this.DMLChange(wSQLText);

			wParams.put("ID", wID);
			wParams.put("WorkTypeID", wWorkTypeID);
			wParams.put("TypeID", wTypeID);
			wParams.put("ProductID", wProductID);
			wParams.put("ResourceID", wResourceID);
			wParams.put("StationID", StringUtils.Join(",", wStationID));
			wParams.put("ResourceCode", wResourceCode);
			wParams.put("Active", wActive);
			wParams.put("ResourceName", StringUtils.isEmpty(wResourceName) ? "" : ("%" + wResourceName + "%"));

			List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParams, wPagination);
			FMCResource wFMCResource = null;
			for (Map<String, Object> wSqlDataReader : wQueryResultList) {
				wFMCResource = new FMCResource();
				wFMCResource.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
				wFMCResource.StationID = StringUtils.parseInt(wSqlDataReader.get("StationID"));
				wFMCResource.StationCode = StringUtils.parseString(wSqlDataReader.get("StationCode"));
				wFMCResource.StationName = StringUtils.parseString(wSqlDataReader.get("StationName"));
				wFMCResource.WorkTypeID = StringUtils.parseInt(wSqlDataReader.get("WorkTypeID"));
				wFMCResource.WorkTypeCode = StringUtils.parseString(wSqlDataReader.get("WorkTypeCode"));
				wFMCResource.WorkTypeName = StringUtils.parseString(wSqlDataReader.get("WorkTypeName"));
				wFMCResource.ProductID = StringUtils.parseInt(wSqlDataReader.get("ProductID"));
				wFMCResource.ProductNo = StringUtils.parseString(wSqlDataReader.get("ProductNo"));
				wFMCResource.ProductName = StringUtils.parseString(wSqlDataReader.get("ProductName"));

				wFMCResource.TypeID = StringUtils.parseInt(wSqlDataReader.get("TypeID"));
				wFMCResource.ResourceID = StringUtils.parseInt(wSqlDataReader.get("ResourceID"));

				if (wFMCResource.TypeID == 1) {

					wFMCResource.Code = StringUtils.parseString(wSqlDataReader.get("EmployeeCode"));
					wFMCResource.Name = StringUtils.parseString(wSqlDataReader.get("EmployeeName"));
				} else if (wFMCResource.TypeID == 2) {

					wFMCResource.Code = StringUtils.parseString(wSqlDataReader.get("TeamCode"));
					wFMCResource.Name = StringUtils.parseString(wSqlDataReader.get("TeamName"));
				}

				wFMCResource.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));
				wFMCResource.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
				wFMCResource.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
				wFMCResource.CreatorName = StringUtils.parseString(wSqlDataReader.get("CreatorName"));
				wFMCResource.EditorName = StringUtils.parseString(wSqlDataReader.get("EditorName"));
				wFMCResource.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
				wFMCResource.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
				wFMCResource.Description = StringUtils.parseString(wSqlDataReader.get("Description"));

				wFMCResourceList.add(wFMCResource);
			}

			// Step03:配置系统
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("FMC_QueryResourceList", ex);
		}
		return wFMCResourceList;
	}

	public FMCResource FMC_QueryResource(BMSEmployee wLoginUser, int wID, OutResult<Integer> wErrorCode) {
		FMCResource wResult = new FMCResource();
		wErrorCode.set(0);

		try {
			List<FMCResource> wList = null;
			if (wID > 0) {
				wList = this.FMC_QueryResourceList(wLoginUser, wID, "", "", null, -1, -1, -1, -1, -1,
						Pagination.getDefault(), wErrorCode);

			}
			if (wList != null && wList.size() > 0) {
				wResult = wList.get(0);
			}
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("FMC_QueryResourceByID", ex);
		}
		return wResult;
	}

	private FMCResource FMC_CheckResource(BMSEmployee wLoginUser, FMCResource wFMCResource,
			OutResult<Integer> wErrorCode) {
		FMCResource wResource = new FMCResource();
		wErrorCode.set(0);

		try {

			// Step0:查询
			Map<String, Object> wParams = new HashMap<String, Object>();
			String wSQLText = StringUtils.Format(
					"Select * from {0}.fmc_resource  where ID!=:ID "
							+ " and (StationID=:StationID AND  WorkTypeID =:WorkTypeID  "
							+ " AND  TypeID =:TypeID AND  ProductID =:ProductID  AND  ResourceID =:ResourceID )  ",
					super.getDBName());
			wParams.clear();
			wParams.put("ID", wFMCResource.ID);
			wParams.put("StationID", wFMCResource.StationID);
			wParams.put("WorkTypeID", wFMCResource.WorkTypeID);
			wParams.put("ProductID", wFMCResource.ProductID);
			wParams.put("TypeID", wFMCResource.TypeID);
			wParams.put("ResourceID", wFMCResource.ResourceID);

			wSQLText = this.DMLChange(wSQLText);
			List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParams);
			for (Map<String, Object> wSqlDataReader : wQueryResultList) {
				wResource.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
				wResource.TypeID = StringUtils.parseInt(wSqlDataReader.get("TypeID"));
				wResource.WorkTypeID = StringUtils.parseInt(wSqlDataReader.get("WorkTypeID"));
				wResource.ProductID = StringUtils.parseInt(wSqlDataReader.get("ProductID"));
				wResource.ResourceID = StringUtils.parseInt(wSqlDataReader.get("ResourceID"));

				wResource.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
				wResource.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
				wResource.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
				wResource.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
				wResource.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));
				wFMCResource.Description = StringUtils.parseString(wSqlDataReader.get("Description"));

			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("FMC_CheckResource", ex);
		}
		return wResource;
	}

}
